package com.glorysoft.xztpda.ui.batchtrackin

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.View
import com.glorysoft.xztpda.entity.ui.ToastMessageBean
import com.glorysoft.xztpda.BR
import com.glorysoft.xztpda.R
import com.glorysoft.xztpda.base.BaseActivity
import com.glorysoft.xztpda.databinding.ActivityBatchTrackInBinding
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.ml.scan.HmsScan
import kotlinx.android.synthetic.main.activity_batch_track_in.*
import kotlinx.android.synthetic.main.activity_batch_track_in.etEqpID
import kotlinx.android.synthetic.main.activity_batch_track_in.etLotID
import kotlinx.android.synthetic.main.activity_batch_track_in.ivEqpScan
import kotlinx.android.synthetic.main.activity_batch_track_in.ivLotScan
import kotlinx.android.synthetic.main.activity_track_in_guide.*

/**
 * FileName: BatchTrackInActivity
 * Author: XJH
 * Date: 2022/4/13 16:53
 * Description:
 * History:
 */
class BatchTrackInActivity : BaseActivity<BatchTrackInViewModel,ActivityBatchTrackInBinding>(),
    View.OnClickListener {
    override fun layoutId(): Int {
        return R.layout.activity_batch_track_in
    }

    override fun initVariableId(): Int {
        return BR.batchTrackInViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("批量进站")
        setRightListener("提交"){
            if (viewModel.lotItems.size <= 0) {
                viewModel.defUI.toastEvent.value = ToastMessageBean("请先添加批次信息",false)
                return@setRightListener
            }

            if (etEqpID.text.toString().isEmpty()) {
                viewModel.defUI.toastEvent.value = ToastMessageBean("请先扫描设备ID",false)
                return@setRightListener
            }

            viewModel.checkEqp(etEqpID.text.toString())
        }

        ivLotScan.setOnClickListener(this)
        ivEqpScan.setOnClickListener(this)
    }

    override fun initData() {
        initEdit(etLotID,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.getLotInfo(s)
                etLotID.setText("")
                Handler().postDelayed({ etLotID.requestFocus() }, 500)
            }
        })

        viewModel.defUI.startActivity.observe(this){
            viewModel.defUI.toastEvent.value = ToastMessageBean("批量进站成功",true)
            finish()
        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.ivLotScan -> startScanForCode(this,10001)
            R.id.ivEqpScan -> startScanForCode(this,10002)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        // 当扫码页面结束后，处理扫码结果。
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK || data == null) {
            return
        }
        if (resultCode == RESULT_OK){
            // 从onActivityResult返回data中，用ScanUtil.RESULT作为key值取到HmsScan返回值。
            when (val obj: Any = data.getParcelableExtra(ScanUtil.RESULT)) {
                is HmsScan -> {
                    val barCode = obj.getOriginalValue()
                    when(requestCode){
                        10001 -> {
//                    etLotID.setText(barCode)
                            viewModel.getLotInfo(barCode)
                        }
                        10002 -> {
                            etEqpID.setText(barCode)
                        }
                    }

                }
            }


        }
    }
}